import axios from 'axios';
import {HOST} from './config';
import Cookies from 'js-cookie';

const http = axios.create({
    timeout: 1000 * 30,
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    },
    baseURL: HOST
});

http.interceptors.request.use(config => {
    config.headers['Authorization'] = Cookies.get('token');
    return config;
}, error => {
    return Promise.reject(error);
});

http.interceptors.response.use(response => {
    // if (response.status === 200) {
    //     router.push('/login');
    // }
    // if (response.data && response.data.code === 401) {
    //     router.push('/login');
    // }
    return response;
}, error => {
    // Cookies.remove('user');
    // Cookies.remove('token');
    // location.reload();
    return Promise.reject(error);
});

export default http;
